import Vue from "vue";
import Router from "vue-router";
import module from "./module";
import Index from "@views/home/Index";
import NotDefined from "@views/NotDefined";
import $store from "../store";
import toLogin from "@libs/login";
import Loading from "@views/Loading";

Vue.use(Router);

const router = new Router({
  mode: "history",
  routes: [
    {
      path: "/login",
      name: "Login",
      meta: {
        title: "登录",
        keepAlive: true,
        backgroundColor: "#27242d"
      },
      component: () => import("@views/user/Login.vue")
    },
    {
      path: "/",
      name: "Index",
      meta: {
        title: "首页",
        keepAlive: true,
        footer: true,
        backgroundColor: "#2a2631"
      },
      component: Index
    },
    {
      path: "/ranking",
      name: "List",
      meta: {
        title: "排行",
        keepAlive: true,
        footer: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/ranking/List.vue")
    },
    {
      path: "/info",
      name: "Info",
      meta: {
        title: "消息中心",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/home/Info.vue")
    },
    {
      path: "/com/edit",
      name: "Edit",
      meta: {
        title: "创建/编辑社群",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/community/Edit.vue")
    },
    {
      path: "/com/home",
      name: "Homepage",
      meta: {
        title: "社群主页",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/community/Homepage.vue")
    },
    {
      path: "/com/articleList",
      name: "ArticleList",
      meta: {
        title: "推文类目名称",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/community/ArticleList.vue")
    },
    {
      path: "/com/articleType",
      name: "ArticleType",
      meta: {
        title: "推文类目编辑",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/community/ArticleType.vue")
    },
    {
      path: "/com/articleTypeAdd",
      name: "ArticleTypeAdd",
      meta: {
        title: "新增分类",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/community/ArticleTypeAdd.vue")
    },
    {
      path: "/com/articleUpdate",
      name: "ArticleUpdate",
      meta: {
        title: "相关推文更新",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/community/ArticleUpdate.vue")
    },
    {
      path: "/com/notice",
      name: "Notice",
      meta: {
        title: "社群公告",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/community/Notice.vue")
    },
    {
      path: "/com/intro",
      name: "Intro",
      meta: {
        title: "社群简介",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/community/Intro.vue")
    },
    {
      path: "/com/introDetail",
      name: "IntroDetail",
      meta: {
        title: "社群简介",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/community/IntroDetail.vue")
    },
    {
      path: "/com/introEdit",
      name: "IntroEdit",
      meta: {
        title: "编辑社群简介",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/community/IntroEdit.vue")
    },
    {
      path: "/com/editAim",
      name: "EditAim",
      meta: {
        title: "编辑社群宗旨",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/community/EditAim.vue")
    },
    {
      path: "/com/devote",
      name: "Devote",
      meta: {
        title: "社群贡献榜",
        keepAlive: true,
        backgroundColor: "#32303a"
      },
      component: () => import("@views/community/Devote.vue")
    },
    {
      path: "/com/link",
      name: "Link",
      meta: {
        title: "关联社群",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/community/Link.vue")
    },
    {
      path: "/com/member",
      name: "Member",
      meta: {
        title: "社群成员",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/community/Member.vue")
    },
    {
      path: "/com/invite",
      name: "Invite",
      meta: {
        title: "邀请加入社群",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/community/Invite.vue")
    },
    {
      path: "/com/apply",
      name: "Apply",
      meta: {
        title: "申请加入社群",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/community/Apply.vue")
    },
    {
      path: "/com/article",
      name: "Article",
      meta: {
        title: "文章详情",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/community/Article.vue")
    },
    {
      path: "/search",
      name: "Search",
      meta: {
        title: "搜索",
        keepAlive: true,
        backgroundColor: "#2a2631"
      },
      component: () => import("@views/ranking/Search.vue")
    },
    {
      path: "/deving",
      name: "Deving",
      meta: {
        title: "开发中",
        keepAlive: true,
        footer: true,
        auth: false
      },
      component: () => import("@views/shop/deving.vue")
    },
    {
      path: "/deving2",
      name: "Deving2",
      meta: {
        title: "开发中",
        keepAlive: true,
        auth: false
      },
      component: () => import("@views/shop/deving2.vue")
    },
    {
      path: "/payResult",
      name: "payResult",
      meta: {
        title: "支付结果",
        keepAlive: true,
        auth: false
      },
      component: () => import("@views/shop/PayResult.vue")
    },
    {
      path: "/auth/:url",
      name: "Loading",
      meta: {
        title: " 加载中",
        keepAlive: true
      },
      component: Loading
    },
    {
      path: "*",
      name: "NotDefined",
      meta: {
        title: "页面找不到",
        keepAlive: true,
        home: false,
        backgroundColor: "#F4F6FB"
      },
      component: NotDefined
    },
    ...module
  ],
  scrollBehavior(to, from) {
    from.meta.scrollTop = window.scrollY;
    return { x: 0, y: to.meta.scrollTop || 0 };
  }
});

const { back, replace } = router;

router.back = function() {
  this.isBack = true;
  back.call(router);
};
router.replace = function(...args) {
  this.isReplace = true;
  replace.call(router, ...args);
};

router.beforeEach((to, form, next) => {
  const { title, backgroundColor, footer, home, auth } = to.meta;
  console.log(to.name, form.name);
  if (auth === true && !$store.state.app.token) {
    if (form.name === "Login") return;
    return toLogin(true, to.fullPath);
  }
  document.title = title || process.env.VUE_APP_NAME || "YSYS商城";
  //判断是否显示底部导航
  footer === true ? $store.commit("SHOW_FOOTER") : $store.commit("HIDE_FOOTER");

  //控制悬浮按钮是否显示
  home === false ? $store.commit("HIDE_HOME") : $store.commit("SHOW_HOME");

  $store.commit("BACKGROUND_COLOR", backgroundColor || "#F5F5F5");

  if (auth) {
    $store.dispatch("USERINFO").then(() => {
      next();
    });
  } else next();
});

export default router;
